#property copyright   "Copyright 2013,"
#property link "mxhe2001@hotmail.com"

#property indicator_chart_window
#property indicator_buffers 5

#define HighHorizontalLine "HighHorizontalLine"
#define LowHorizontalLine "LowHorizontalLine"
#define StartVerticalLine "StartVerticalLine"
#define EndVerticalLine "EndVerticalLine"
#define INFOLine "InfoLine"
#define RTGUp "RTGUp"
#define RTGDown "RTGDown"
#define OpenLabel "OpenLabel"
#define RTGUpLabel "RTGUpLabel"
#define RTGDownLabel "RTGDownLabel"
#define SwingLabel "SwingLabel"
#define RTGLabel "RTGLabel"

extern int DayPeriod = 14;
extern string time = "22:00";
extern int GMT.offHour=0;
extern int lineWidth = 3;
extern bool showALInfo = true;

double OpenPrice;

double RTGupBuffer[];
double RTGdownBuffer[];
double SwingBuffer[];
double HighBuffer[];
double LowBuffer[];

int init()
{
    SetIndexBuffer(0,RTGupBuffer);
    SetIndexBuffer(1,RTGdownBuffer);
    SetIndexBuffer(2,SwingBuffer);
    SetIndexBuffer(3,HighBuffer);
    SetIndexBuffer(4,LowBuffer);
    SetIndexLabel(0,"RTGup");
    SetIndexLabel(1,"RTGdown");
    SetIndexLabel(2,"Swing");
    SetIndexLabel(3,"High");
    SetIndexLabel(4,"Low");
    start();
}

void deinit() 
{
    for (int i = ObjectsTotal() - 1; i >= 0; i--)
    {
        if (StringFind(ObjectName(i), HighHorizontalLine) >= 0 ||
            StringFind(ObjectName(i), LowHorizontalLine) >= 0 ||
            StringFind(ObjectName(i), StartVerticalLine) >= 0 ||
            StringFind(ObjectName(i), INFOLine) >= 0 ||
            StringFind(ObjectName(i), EndVerticalLine) >= 0)
        {
            ObjectDelete(ObjectName(i));
        }
    }  
    
    ObjectDelete(RTGUp);
    ObjectDelete(RTGDown);   
    ObjectDelete(OpenLabel);
    ObjectDelete(RTGUpLabel);
    ObjectDelete(RTGDownLabel);
    ObjectDelete(SwingLabel);
    ObjectDelete(RTGLabel);
    
    return(0);
}

//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
{
    int count = 0;
    double tmpRGT[];
    ArrayResize(tmpRGT, DayPeriod);
    for (int i = 0; count < DayPeriod;i++)
    {
        datetime startTime = StrToTime(TimeToStr(TimeCurrent() - 24*(i+1)*60*60, TIME_DATE) + " " + time) - GMT.offHour*60*60;
        datetime endTime = StrToTime(TimeToStr(TimeCurrent()- 24*i*60*60, TIME_DATE) + " " + time) - GMT.offHour*60*60;
                
        int startBar = iBarShift(NULL,0,startTime);
        int endBar = iBarShift(NULL,0,endTime);
        
        //Print("111 i:",i, " startTime:", TimeToStr(startTime), " endTime:",TimeToStr(endTime), " startBar:", TimeToStr(Time[startBar]), " endBar:",TimeToStr(Time[endBar]));
        
        while (TimeDay(endTime) != TimeDay(Time[endBar]))
        {
            i++;
            startTime = StrToTime(TimeToStr(TimeCurrent() - 24*(i+1)*60*60, TIME_DATE) + " " + time) - GMT.offHour*60*60;
            endTime = StrToTime(TimeToStr(TimeCurrent()- 24*i*60*60, TIME_DATE) + " " + time) - GMT.offHour*60*60;
            startBar = iBarShift(NULL,0,startTime);
            endBar = iBarShift(NULL,0,endTime);
        }
        
        while (TimeDay(startTime) != TimeDay(Time[startBar]))
        {
            i++;
            startTime = StrToTime(TimeToStr(TimeCurrent() - 24*(i+1)*60*60, TIME_DATE) + " " + time) - GMT.offHour*60*60;
            startBar = iBarShift(NULL,0,startTime);
        }
        
        if (TimeDayOfWeek(endTime) == 0 && TimeDayOfWeek(startTime) == 5)
        {
            continue;
        }


        if (Time[startBar] < StrToTime(TimeToStr(startTime, TIME_DATE) + " " + time) - GMT.offHour*60*60)
        {
            startBar--;
        }
        
        if (Time[endBar] < StrToTime(TimeToStr(endTime, TIME_DATE) + " " + time) - GMT.offHour*60*60)
        {
            endBar--;
        }
        
        endBar = endBar + 1;   
        
        //Print("222 i:",i, " startTime:", TimeToStr(startTime), " endTime:",TimeToStr(endTime), " startBar:", TimeToStr(Time[startBar]), " endBar:",TimeToStr(Time[endBar]));
        
        
        double highPrice = High[iHighest(NULL,0,MODE_HIGH, startBar - endBar + 1, endBar)];
        double lowPrice = Low[iLowest(NULL,0,MODE_LOW, startBar - endBar + 1, endBar)];
        double tOpenPrice = Open[startBar]; 
        
        
        
        if (count == 0)
        {
            OpenPrice = tOpenPrice;
            HighBuffer[0] = highPrice; 
            LowBuffer[0] = lowPrice; 
        }    
        
        if (showALInfo)
        {
            drawTrendLine(HighHorizontalLine + count, highPrice, startTime, endTime, Orange);     
            drawTrendLine(LowHorizontalLine + count, lowPrice, startTime, endTime, Orange);
       
            drawVLine(StartVerticalLine + count, startTime, Red);     
            drawVLine(EndVerticalLine + count, endTime, White);
        }
        else
        {
            ObjectDelete(HighHorizontalLine + count);
            ObjectDelete(LowHorizontalLine + count);
            ObjectDelete(StartVerticalLine + count);
            ObjectDelete(EndVerticalLine + count);
        }
                
        double OH = (highPrice - tOpenPrice)/Point;  
        double OL = (lowPrice - tOpenPrice)/Point; 
        double AL = MathMax(OH, -OL);  
        if (showALInfo)
        {
            string strText = "OH:" + DoubleToStr(OH, 0) + " OL:" + DoubleToStr(OL, 0) + " AL:" + DoubleToStr(AL, 0);
            drawText(INFOLine + count, strText, Time[(startBar + endBar)/2], lowPrice - 50*Point);
        }
        else
        {
            ObjectDelete(INFOLine + count);
        }
        tmpRGT[count] = AL;
        count++;
     }

    double RTG = iMAOnArray(tmpRGT,0,DayPeriod,0,MODE_SMA,0);
    double SWING = RTG*3/4;
    
    double RTGDownValue = OpenPrice - RTG*Point - 10*Point;
    double RTGUpValue = OpenPrice + RTG*Point + 10*Point;   
    
     if (ObjectFind(RTGUp) == -1)
     {
         ObjectCreate(RTGUp, OBJ_HLINE, 0, 0, 0);
         ObjectSet(RTGUp, OBJPROP_COLOR, Green);
         ObjectSet(RTGUp, OBJPROP_WIDTH, lineWidth);
     }
     
     ObjectSet(RTGUp, OBJPROP_PRICE1, RTGUpValue);
     ObjectSet(RTGUp, OBJPROP_PRICE2, RTGUpValue);
     
     if (ObjectFind(RTGDown) == -1)
     {
         ObjectCreate(RTGDown, OBJ_HLINE, 0, 0, 0);
         ObjectSet(RTGDown, OBJPROP_COLOR, Green);
         ObjectSet(RTGDown, OBJPROP_WIDTH, lineWidth);
     }
     
     ObjectSet(RTGDown, OBJPROP_PRICE1, RTGDownValue);
     ObjectSet(RTGDown, OBJPROP_PRICE2, RTGDownValue); 
     
     drawLabel(OpenLabel, "Open:" + DoubleToStr(OpenPrice, Digits), 20 ,40);
     drawLabel(RTGUpLabel, "RTGUp:" + DoubleToStr(RTGUpValue, Digits), 20 ,70);
     drawLabel(RTGDownLabel, "RTGDown:" + DoubleToStr(RTGDownValue, Digits), 20 ,100);
     drawLabel(SwingLabel, "Swing:" + DoubleToStr(SWING, 0), 20 ,130);
     drawLabel(RTGLabel, "RTG:" + DoubleToStr(RTG, 0), 20 ,160);
     
     RTGupBuffer[0] = RTGUpValue;
     RTGdownBuffer[0] = RTGDownValue;
     SwingBuffer[0] = SWING; 
}

void drawText(string name, string text, datetime time, double price)
{
    if (ObjectFind(name) == -1)
     {
         ObjectCreate(name, OBJ_TEXT, 0, 0, 0);         
         ObjectSet(name, OBJPROP_PRICE1, price);
         ObjectSet(name, OBJPROP_TIME1, time);
     }  
     
     ObjectSetText(name, text, 7, "Times New Roman", Magenta);  
}

void drawLabel(string name, string text, int x, int y)
{
    if (ObjectFind(name) == -1)
     {
         ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
         ObjectSet(name, OBJPROP_XDISTANCE, x);
         ObjectSet(name, OBJPROP_YDISTANCE, y);
     }  
     ObjectSetText(name, text, 12, "Times New Roman", Yellow);   
}

void drawVLine(string name, datetime time, color lineColor)
{
    if (ObjectFind(name) == -1)
     {
         ObjectCreate(name, OBJ_VLINE, 0, 0, 0);
         ObjectSet(name, OBJPROP_COLOR, lineColor);
         ObjectSet(name, OBJPROP_WIDTH, 2);
     }
     
     ObjectSet(name, OBJPROP_TIME1, time);
     ObjectSet(name, OBJPROP_TIME2, time);
}

void drawTrendLine(string name, double price, datetime startTime, datetime endTime, color lineColor)
{
    if (ObjectFind(name) == -1)
    {
        ObjectCreate(name, OBJ_TREND, 0, 0, 0);
        ObjectSet(name, OBJPROP_COLOR, lineColor);
        ObjectSet(name, OBJPROP_WIDTH, 2);
        ObjectSet(name, OBJPROP_RAY, false);
    }

    ObjectSet(name, OBJPROP_PRICE1, price);
    ObjectSet(name, OBJPROP_PRICE2, price);
    ObjectSet(name, OBJPROP_TIME1, startTime);
    ObjectSet(name, OBJPROP_TIME2, endTime);
}